package com.lingyi.yygh.common.handle;


import com.lingyi.yygh.common.exception.YyghException;
import com.lingyi.yygh.common.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName GlobalExceptionHandler
 * @Description 异常处理类
 * @Author liujie
 * @Date 2021/8/31 20:14
 * @Version 1.0
 **/
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 统一异常处理类
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.fail().message("执行了统一异常");
    }

    /**
     * 特定异常处理类
     * @param e
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e){
        e.printStackTrace();
        return Result.fail().message("执行了特定异常");
    }

    /**
     * 自定义异常处理类
     * @param e
     * @return
     */
    @ExceptionHandler(YyghException.class)
    @ResponseBody
    public Result error(YyghException e){
        e.printStackTrace();
        return Result.fail().message(e.getMessage()).code(e.getCode());
    }
}
